"""
存放数据库文件，与数据库打交道
"""
import os
import json
from conf import settings

# 查看用户数据
def select(username):
    # 1) 接收接口层传过来的username用户名，拼接用户json文件路径
    user_data = os.path.join(
        settings.USER_PATH,f'{username}.json'
    )

    # 2) 校验用户json文件是否存在
    if os.path.exists(user_data):
        with open(user_data,'r',encoding='utf-8') as f:
            user_dic = json.load(f)

            return user_dic

# 保存数据
def save(user_dic):
    username = user_dic.get('username')

    user_data = os.path.join(
        settings.USER_PATH, f'{username}.json'
    )

    with open(user_data,'w',encoding='utf-8') as f:
        json.dump(user_dic,f,ensure_ascii=False)



